<?php declare(strict_types=1);

/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Develop\Ctl;

use Srv\Apps\Proto\DTO\Develop\AppVersionAddDTO;
use Srv\Apps\Proto\DTO\Develop\AppVersionEditDTO;
use Srv\Apps\Proto\DTO\Develop\AppVersionListDTO;
use Srv\Apps\Proto\Enum\YesOrNoEnum;
use Srv\Apps\Proto\VO\Develop\AppVersionListVO;
use Srv\Apps\Proto\VO\Develop\AppVersionInfoVO;
use Srv\Libs\Common\CommArray;
use Srv\Libs\Common\CommTime;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Frame\Page;
use Srv\Libs\Storage\DataMysql;

final class AppVersionCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;     // DB-Write

    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
        }
        return $thisObject;
    }

    /**
     * @param int $vrlrId
     * @return array
     * getAppVersionSrc
     */
    public function getAppVersionSrc(int $vrlrId):array
    {
        if($vrlrId < 1) return [];
        $isFail             = false;
        $appVersionRecord   = $this->DbAuto->getDataOne('SELECT vrlr_id, ddlt_id, vrlr_packname, vrlr_version, vrlr_platform, vrlr_title, vrlr_desc, vrlr_download_url, vrlr_level, vrlr_release, vrlr_release_time, vrlr_create_time, vrlr_update_time FROM '.$this->DbAuto->getTableNameAll('version_release_record').' WHERE vrlr_id='.$vrlrId.' AND vrlr_delete=0', $isFail);
        if($isFail || !isset($appVersionRecord['vrlr_id'])) return [];
        return $appVersionRecord;
    }

    /**
     * @param Page $Page
     * @param array $whereData
     * @return AppVersionListVO|null
     * getAppVersionList
     */
    public function getAppVersionList(Page $Page, array $whereData): ?AppVersionListVO
    {
        $limitString        = $Page->getLimitString();
        $whereString        = ' WHERE vrlr_delete=0 ';
        // 检索条件
        if(count($whereData) > 0)foreach ($whereData as $key => $val){
            switch ($key){
                case AppVersionListDTO::_vrlrId:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'vrlr_id='. intval($val);
                    break;
                case AppVersionListDTO::_packName:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'vrlr_packname LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case AppVersionListDTO::_version:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'vrlr_version LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case AppVersionListDTO::_platform:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'vrlr_platform LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case AppVersionListDTO::_title:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'vrlr_title LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case AppVersionListDTO::_level:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'vrlr_level='. intval($val);
                    break;
                case AppVersionListDTO::_release:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'vrlr_release='. intval($val);
                    break;
            }
        }
        $isFail                 = false;
        $sql                    = 'SELECT SQL_CALC_FOUND_ROWS vrlr_id, ddlt_id, vrlr_packname, vrlr_version, vrlr_platform, vrlr_title, vrlr_desc, vrlr_download_url, vrlr_level, vrlr_release, vrlr_release_time, vrlr_create_time, vrlr_update_time FROM '.$this->DbAuto->getTableNameAll('version_release_record').$whereString.' ORDER BY vrlr_id DESC '.$limitString;
        $appVersionRecordList   = $this->DbAuto->getData($sql, $isFail);
        $totalNum               = $this->DbAuto->getDataInt('SELECT FOUND_ROWS() as num', 'num');
        if($isFail) return null;
        $AppVersionInfoVOList   = [];
        if(CommArray::isForeach($appVersionRecordList))foreach($appVersionRecordList as $appVersionRecord){
            $vrlrIdItem             = intval($appVersionRecord['vrlr_id']);
            if($vrlrIdItem < 1) continue;
            $AppVersionInfoVO       = $this->getAppVersionInfoVO($appVersionRecord);
            if(!($AppVersionInfoVO instanceof AppVersionInfoVO)) continue;
            $AppVersionInfoVOList[] = $AppVersionInfoVO;
        }
        $AppVersionListVO           = new AppVersionListVO();
        $AppVersionListVO->setList($AppVersionInfoVOList);
        $AppVersionListVO->setPage($Page->getPageVO($totalNum));
        return $AppVersionListVO;
    }

    /**
     * @param int $vrlrId
     * @return AppVersionInfoVO|null
     * getAppVersionInfo
     */
    public function getAppVersionInfo(int $vrlrId):?AppVersionInfoVO
    {
        $appVersionRecord   = $this->getAppVersionSrc($vrlrId);
        if(!isset($appVersionRecord['vrlr_id'])) return null;
        return $this->getAppVersionInfoVO($appVersionRecord);
    }

    /**
     * @param array $appVersionRecord
     * @return AppVersionInfoVO|null
     * getAppVersionInfoVO
     */
    public function getAppVersionInfoVO(array $appVersionRecord): ?AppVersionInfoVO
    {
        $vrlrId             = intval($appVersionRecord['vrlr_id']??0);
        if($vrlrId < 1) return null;
        $AppVersionInfoVO   = new AppVersionInfoVO();
        $AppVersionInfoVO->setVrlrId($vrlrId);
        $AppVersionInfoVO->setDdltId(intval($appVersionRecord['ddlt_id']));
        $AppVersionInfoVO->setPackName($appVersionRecord['vrlr_packname']);
        $AppVersionInfoVO->setVersion(trim($appVersionRecord['vrlr_version']));
        $AppVersionInfoVO->setPlatform(trim($appVersionRecord['vrlr_platform']));
        $AppVersionInfoVO->setTitle(trim($appVersionRecord['vrlr_title']));
        $AppVersionInfoVO->setDesc(trim($appVersionRecord['vrlr_desc']));
        $AppVersionInfoVO->setDownloadUrl(trim($appVersionRecord['vrlr_download_url']));
        $AppVersionInfoVO->setLevel(intval($appVersionRecord['vrlr_level']));
        $AppVersionInfoVO->setRelease(intval($appVersionRecord['vrlr_release']));
        $AppVersionInfoVO->setReleaseTime(intval($appVersionRecord['vrlr_release_time']));
        $AppVersionInfoVO->setCreateTime(intval($appVersionRecord['vrlr_create_time']));
        $AppVersionInfoVO->setUpdateTime(intval($appVersionRecord['vrlr_update_time']));
        return $AppVersionInfoVO;
    }

    /**
     * @param array $setData
     * @return int
     * appVersionAdd
     */
    public function appVersionAdd(array $setData):int
    {
        if(count($setData) < 1) return 0;
        $currTime   = CommTime::getTimeStamp();
        $setString  = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case AppVersionAddDTO::_ddltId:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'ddlt_id='.intval($val);
                    break;
                case AppVersionAddDTO::_packName:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_packname=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AppVersionAddDTO::_version:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_version=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AppVersionAddDTO::_platform:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_platform=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AppVersionAddDTO::_title:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_title=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AppVersionAddDTO::_desc:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_desc=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AppVersionAddDTO::_downloadUrl:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_download_url=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AppVersionAddDTO::_level:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_level='.intval($val);
                    break;
                case AppVersionAddDTO::_release:
                    $release    = intval($val);
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_release='.$release;
                    if($release == YesOrNoEnum::YES_OR_NO_YES) $setString .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_release_time='.$currTime;
                    break;
            }
        }
        if(strlen($setString) < 1) return 0;
        $isFail     = false;
        $sql        = 'INSERT INTO '.$this->DbAuto->getTableNameAll('version_release_record').' SET '.$setString.', vrlr_create_time='.$currTime.', vrlr_update_time='.$currTime;
        $vrlrId     = $this->DbAuto->getDataId($sql, $isFail);
        if($isFail || $vrlrId < 1) return 0;
        return $vrlrId;
    }

    /**
     * @param int $vrlrId
     * @param array $setData
     * @return bool
     * appVersionEdit
     */
    public function appVersionEdit(int $vrlrId, array $setData):bool
    {
        if($vrlrId < 1 || count($setData) < 1) return false;
        $currTime       = CommTime::getTimeStamp();
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case AppVersionEditDTO::_ddltId:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'ddlt_id='.intval($val);
                    break;
                case AppVersionEditDTO::_packName:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_packname=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AppVersionEditDTO::_version:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_version=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AppVersionEditDTO::_platform:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_platform=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AppVersionEditDTO::_title:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_title=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AppVersionEditDTO::_desc:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_desc=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AppVersionEditDTO::_downloadUrl:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_download_url=\''.$this->DbAuto->getRealEscapeString($val) .'\'';
                    break;
                case AppVersionEditDTO::_level:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_level='.intval($val);
                    break;
                case AppVersionEditDTO::_release:
                    $release    = intval($val);
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_release='.$release;
                    if($release == YesOrNoEnum::YES_OR_NO_YES) $setString .= (strlen($setString) > 0 ? ', ' : '') . 'vrlr_release_time='.$currTime;
                    break;
            }
        }
        if(strlen($setString) < 1) return false;
        $currTime   = CommTime::getTimeStamp();
        $isFail     = false;
        $sql        = 'UPDATE '.$this->DbAuto->getTableNameAll('version_release_record').' SET '.$setString.', vrlr_update_time='.$currTime.' WHERE vrlr_id='.$vrlrId.' AND vrlr_delete=0';
        $this->DbAuto->getDataNum($sql, $isFail);
        if($isFail) return false;
        return true;
    }
}